package com.activiti;

import com.redpig.App;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.engine.*;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.history.HistoricActivityInstanceQuery;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.image.ProcessDiagramGenerator;
import org.activiti.image.impl.DefaultProcessDiagramGenerator;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.zip.ZipInputStream;

@SpringBootTest(classes = App.class)
public class ActivitiHistory {

    @Autowired
    RepositoryService repositoryService;

    @Autowired
    RuntimeService runtimeService;

    @Autowired
    TaskService taskService;

    @Autowired
    HistoryService historyService;

    @Test
    public void test5(){

        //获取Actinst表查询对象
        HistoricActivityInstanceQuery historicActivityInstanceQuery = historyService.createHistoricActivityInstanceQuery();
        //historicActivityInstanceQuery.processDefinitionId("evection:1:12540");

        historicActivityInstanceQuery.orderByHistoricActivityInstanceStartTime().desc();
        List<HistoricActivityInstance> list=historicActivityInstanceQuery.list();
        //输出查询结果
        for(HistoricActivityInstance hi:list){
            System.out.println("hi.getActivityId() = " + hi.getActivityId());
            System.out.println("hi.getActivityName() = " + hi.getActivityName());
            System.out.println("hi.getActivityType() = " + hi.getActivityType());
            System.out.println("hi.getAssignee() = " + hi.getAssignee());
            System.out.println("hi.getProcessDefinitionId() = " + hi.getProcessDefinitionId());
            System.out.println("hi.getProcessInstanceId() = " + hi.getProcessInstanceId());
            System.out.println("------------------------");
        }

    }

    /**查询流程状态（判断流程正在执行，还是结束）*/
    @Test
    public void isProcessEnd(){
        String processInstanceId = "9f9c37e3-255f-11ee-bbb1-3413e841d179";
        //去正在执行的任务表查询
        ProcessInstance pi = runtimeService//表示正在执行的流程实例和执行对象
                .createProcessInstanceQuery()//创建流程实例查询
                .processInstanceId(processInstanceId)//使用流程实例ID查询
                .singleResult();
        if(pi==null){
            System.out.println("该流程实例走完");
        }
        else{
            System.out.println("该流程实例还没走完");
        }
//      输出：
//      该流程实例还没走完
    }
}
